package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.bo.DevOnlineRecordBo;
import com.bdmc.device.domain.vo.DevOnlineRecordVo;
import com.bdmc.device.domain.DevOnlineRecord;
import com.bdmc.device.mapper.DevOnlineRecordMapper;
import com.bdmc.device.service.IDevOnlineRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * gps设备在线记录Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-22
 */
@Service
public class DevOnlineRecordServiceImpl extends ServicePlusImpl<DevOnlineRecordMapper, DevOnlineRecord, DevOnlineRecordVo> implements IDevOnlineRecordService {

    @Override
    public DevOnlineRecordVo queryById(Long onlineRecordId){
        return getVoById(onlineRecordId);
    }

    @Override
    public TableDataInfo<DevOnlineRecordVo> queryPageList(DevOnlineRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevOnlineRecord> lqw = buildQueryWrapper(bo);
        Page<DevOnlineRecordVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevOnlineRecordVo> mySelectPage(DevOnlineRecordBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevOnlineRecordVo> queryList(DevOnlineRecordBo bo) {
        LambdaQueryWrapper<DevOnlineRecord> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevOnlineRecord> buildQueryWrapper(DevOnlineRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DevOnlineRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, DevOnlineRecord::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getDevicePhoneNum()), DevOnlineRecord::getDevicePhoneNum, bo.getDevicePhoneNum());
        lqw.eq(bo.getStartTime() != null, DevOnlineRecord::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, DevOnlineRecord::getEndTime, bo.getEndTime());
        lqw.eq(bo.getOnlineState() != null, DevOnlineRecord::getOnlineState, bo.getOnlineState());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevOnlineRecordBo bo) {
        DevOnlineRecord add = BeanUtil.toBean(bo, DevOnlineRecord.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setOnlineRecordId(add.getOnlineRecordId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevOnlineRecordBo bo) {
        DevOnlineRecord update = BeanUtil.toBean(bo, DevOnlineRecord.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevOnlineRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
